import 'dart:ui';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:guji/global.dart';
import 'package:guji/manager/main_manager.dart';
import 'package:guji/pages/detail.dart';
import 'package:guji/pages/edit.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';

class HomePage extends StatefulWidget {
  HomePage({Key key}) : super(key: key);
  @override
  State<StatefulWidget> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  void initState() {
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark);
    super.initState();
  }

  List<Widget> renderList(data) {
    List<Widget> list = [];
    for (var i = 0; i < data.length; i++) {
      var item = data[i];
      var date = DateTime.fromMillisecondsSinceEpoch(item['id']);
      var formatter = new DateFormat('yyyy年M月d日');
      String formatted = formatter.format(date);
      list.add(
        InkWell(
          onTap: () {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => DetailPage(id: item['id']),
              ),
            );
          },
          child: Container(
            width: MediaQuery.of(context).size.width,
            padding: EdgeInsets.all(15),
            margin: EdgeInsets.all(10),
            decoration: BoxDecoration(
              color: Global.backgroundColor,
              boxShadow: [
                BoxShadow(
                  color: Color(0xFF000000).withOpacity(0.06),
                  offset: Offset(-1, 10),
                  blurRadius: 15,
                  spreadRadius: 1,
                )
              ],
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                Text(
                  formatted,
                  style: TextStyle(
                    color: Global.fontSecondColor,
                    fontSize: 14,
                    fontFamily: 'FangZhengShuSongFanTi',
                    fontWeight: FontWeight.w600,
                  ),
                ),
                SizedBox(height: 6),
                Text(
                  item['title'],
                  style: TextStyle(
                    color: Global.fontColor,
                    fontSize: 18,
                    fontFamily: 'FangZhengShuSongFanTi',
                    fontWeight: FontWeight.w600,
                  ),
                  maxLines: 3,
                ),
                SizedBox(height: 6),
                Text(
                  item['data'],
                  style: TextStyle(
                    color: Global.fontColor.withOpacity(.8),
                    fontSize: 14,
                    fontFamily: 'FangZhengShuSongFanTi',
                    fontWeight: FontWeight.w600,
                  ),
                  maxLines: 3,
                ),
              ],
            ),
          ),
        ),
      );
    }
    return list;
  }

  @override
  Widget build(BuildContext context) {
    MainManager _mainManager = Provider.of<MainManager>(context);

    return Scaffold(
      body: Container(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        decoration: BoxDecoration(
          color: Global.cardBackgroundColor,
          image: DecorationImage(
            image: AssetImage("assets/images/index.png"),
            repeat: ImageRepeat.repeat,
            fit: BoxFit.fill,
          ),
        ),
        padding: EdgeInsets.fromLTRB(
          20,
          Global.padding.top,
          20,
          Global.padding.bottom + 20,
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Text(
                  '简记',
                  style: TextStyle(
                    fontSize: 20,
                    fontWeight: FontWeight.w800,
                    fontFamily: 'FangZhengShuSongFanTi',
                    color: Global.fontColor,
                  ),
                ),
              ],
            ),
            _mainManager.allLocalCache.length > 0
                ? Expanded(
                    child: SingleChildScrollView(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: renderList(_mainManager.allLocalCache),
                      ),
                    ),
                  )
                : Expanded(
                    child: Container(
                      child: Image.asset('assets/images/bg.png'),
                    ),
                  ),
            InkWell(
              onTap: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => EditPage(),
                  ),
                );
              },
              child: Container(
                width: MediaQuery.of(context).size.width,
                padding: EdgeInsets.all(10),
                decoration: BoxDecoration(
                  color: Global.primaryColor,
                  gradient: LinearGradient(
                    begin: Alignment.topLeft,
                    end: Alignment.bottomRight,
                    colors: [
                      Global.gradientEndColor,
                      Global.primaryColor,
                    ],
                  ),
                ),
                child: Center(
                  child: Text(
                    '编写简记',
                    style: TextStyle(
                      fontSize: 20,
                      fontWeight: FontWeight.w800,
                      fontFamily: 'FangZhengShuSongFanTi',
                      color: Global.backgroundColor,
                    ),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
